package com.lyl.mp.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.lyl.mp.mybatisplus.entity.Student;
import com.lyl.mp.mybatisplus.mapper.StudentMapper;
import com.lyl.mp.mybatisplus.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lvxiaobu
 * @since 2023-02-22
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Resource
    StudentMapper studentMapper;

    @Override
    public List<Student> selectByName(String name) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>(Student.class);
        wrapper.eq(Student::getName,name);
        return list(wrapper);
    }

    @Override
    public void update(String id, String address) {
        LambdaUpdateWrapper<Student> wrapper = new LambdaUpdateWrapper<Student>(Student.class);
        wrapper.eq(Student::getId,id);
        wrapper.set(Student::getAddress,address);
        update(wrapper); // 仅通过wrapper更新
    }

    @Override
    public void update(String name, String address, String phone) {
        LambdaUpdateWrapper<Student> wrapper = new LambdaUpdateWrapper<Student>(Student.class);
        wrapper.eq(Student::getName,name);
        Student s = new Student();
        s.setAddress(address);
        s.setPhone(phone);
        update(s,wrapper); // wrapper只放入where条件,entity放需要更新的字段内容
    }

    @Override
    public void deleteByModifyFlag(String name, String address) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<Student>(Student.class);
        wrapper.eq(Student::getName,name);
        wrapper.eq(Student::getAddress,address);
        boolean flag = remove(wrapper);
        System.out.println("deleteByModifyFlag------：remove成功");
    }


}
